package com.yonder.hashmaptest;

import java.util.HashMap;
import java.util.Map;

import io.netty.util.collection.LongObjectHashMap;

/**
 * 
 * @author KXZ 
 * 2018年5月14日
 */
public class PeriodHashMapTest {
	
	
	public static void main(String[] args) throws InterruptedException {
//		Thread.sleep(30000);
		DebugTools.printMemUsed();
		System.out.println();
		int num = 1000000;
		System.out.println("以下测试分别为"+num+"个Map<Long, Integer>添加元素数的内存使用量\n");
		int addNum=7,capacity = 8;
		PeriodHashMap<Integer>[] maps = new PeriodHashMap[num];
		for (int i = 0; i < num; i++) {
			maps[i] = new PeriodHashMap<>();
//			for (int j = 0; j < addNum; j++) {
//				maps[i].put(j*1l , (Integer)j);
//			}
		}
		DebugTools.printMemUsed();
		System.out.println(PeriodHashMap.class.getSimpleName()+"添加元素数/容量:"+addNum+"/"+capacity+" ↑\n");
		
		HashMap<Long, Integer>[] maps1 = new HashMap[num];
		for (int i = 0; i < num; i++) {
			maps1[i] = new HashMap<>();
//			for (int j = 0; j < addNum; j++) {
//				maps1[i].put(j*1l , j);
//			}
		}
		DebugTools.printMemUsed();
		System.out.println(HashMap.class.getSimpleName()+"添加元素数/容量:"+addNum+"/"+capacity+" ↑\n");
		
		addNum = 3;
		PeriodHashMap<Integer>[] maps3 = new PeriodHashMap[num];
		for (int i = 0; i < num; i++) {
			maps[i] = new PeriodHashMap<>();
			for (int j = 0; j < addNum; j++) {
				maps[i].put(j*1l , (Integer)j);
			}
		}
		DebugTools.printMemUsed();
		System.out.println(PeriodHashMap.class.getSimpleName()+"添加元素数/容量:"+addNum+"/"+capacity+" ↑\n");
		
		HashMap<Long, Integer>[] maps4 = new HashMap[num];
		for (int i = 0; i < num; i++) {
			maps1[i] = new HashMap<>(capacity);
			for (int j = 0; j < addNum; j++) {
				maps1[i].put(j*1l , j);
			}
		}
		DebugTools.printMemUsed();
		System.out.println(HashMap.class.getSimpleName()+"添加元素数/容量:"+addNum+"/"+capacity+" ↑\n");
		
		
//		System.out.println(maps.length+","+maps1.length+","+maps3.length+","+maps4.length);
		
		long period = 10000;
		System.out.println("以下测试key值间隔为:"+period+" Map<Long, Integer>容量由初始8开始添加10000个元素, put和get分别测试1亿次");
		PeriodHashMap.setPeriod(period);
		PeriodHashMap<Integer> map2 = new PeriodHashMap<>();
		testMap(map2, period);
		HashMap<Long, Integer> map = new HashMap<>();
		testMap(map, period);
		LongObjectHashMap<Integer> map1 = new LongObjectHashMap<>();
		testMap(map1, period);
		Thread.sleep(30000);
	}
	
	static void testMap(Map<Long, Integer> map, long period) {
		long cnt = 0;
		long now = System.currentTimeMillis();
		for (int i = 0; i < 10000; i++) {
			for (int j = 0; j < 10000; j++) {
				map.put((j)*period+1, i);
			}
		}
		System.out.println("put:(ms)"+(System.currentTimeMillis()-now));
		now = System.currentTimeMillis();
		for (int i = 0; i < 10000; i++) {
			for (int j = 0; j < 10000; j++) {
				cnt += map.get((j)*period+1);
			}
		}
		System.out.println("get:(ms)"+(System.currentTimeMillis()-now));
		System.out.println(map.getClass().getSimpleName()+", "+cnt+"\n");
	}
	static void testMap(LongObjectHashMap<Integer> map, long period) {
		long cnt = 0;
		long now = System.currentTimeMillis();
		for (int i = 0; i < 10000; i++) {
			for (int j = 0; j < 10000; j++) {
				map.put((j)*period+1, (Integer)i);
			}
		}
		System.out.println("put:(ms)"+(System.currentTimeMillis()-now));
		now = System.currentTimeMillis();
		for (int i = 0; i < 10000; i++) {
			for (int j = 0; j < 10000; j++) {
				cnt += map.get((j)*period+1);
			}
		}
		System.out.println("get:(ms)"+(System.currentTimeMillis()-now));
		System.out.println(map.getClass().getSimpleName()+","+cnt+"\n");
	}
	
	static void testMap(PeriodHashMap<Integer> map, long period) {
		long cnt = 0;
		long now = System.currentTimeMillis();
		for (int i = 0; i < 10000; i++) {
			for (int j = 0; j < 10000; j++) {
				map.put((j)*period+1, (Integer)i);
			}
		}
		System.out.println("put:(ms)"+(System.currentTimeMillis()-now));
		now = System.currentTimeMillis();
		for (int i = 0; i < 10000; i++) {
			for (int j = 0; j < 10000; j++) {
				cnt += map.get((j)*period+1);
			}
		}
		System.out.println("get:(ms)"+(System.currentTimeMillis()-now));
		System.out.println(map.getClass().getSimpleName()+","+cnt+"\n");
	}
}
